package org.lanqiao.tjut.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.tjut.bean.TBAdminsBean;
import org.lanqiao.tjut.model.ModelFactory;
import org.lanqiao.tjut.myUtils.MyUtils;


public class UserLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
	/**
	 * 初始化方法
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * 销毁方法
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * doget--接收客户端发送的request请求
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			           throws ServletException, IOException {
		// 进项客户端请求处理
		System.out.println("这里是进行客户端request请求的处理。。。");
		//使用request对象的getParameter方法可以获取客户端使用http的request请求提交给服务器的form表单的数据内容
		//获取使用控件的那么属性获取
		//获取姓名
		String strName = request.getParameter("txtUserName");
		//获取密码
		String strPsw = request.getParameter("txtUserPsw");
		System.out.println("获取客户端发送的用户名："+strName);
		System.out.println("获取客户端发送的，密码："+strPsw);
		
		//使用实体类进行传参
		TBAdminsBean adminB = MyUtils.getNewInstance(TBAdminsBean.class);
		//将前台页面传入值，复制给实体变量
		adminB.setAdmins_name(strName);
		adminB.setAdmins_psw(strPsw);
		
		//调用model层进行登录验证的逻辑处理
		//获取登录用户信息
		List<TBAdminsBean> lstAdmins = ModelFactory.getUserLoginModelInstance().getUserLoginInfo(adminB);
		
		//跳转到登录成功的页面，页面跳转之后代码不会被执行
		//判定是否登录成功
		if(lstAdmins != null&& lstAdmins.size()>0){
			//登录成功
			response.sendRedirect("logined/logined.jsp");
		}else{
			//登录失败
			response.sendRedirect("login.jsp");
		}
	}

	/**
	 * dopost---使用response响应客户端请求
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
